
package view;

import controller.Basket;
import javax.inject.Named;
import javax.enterprise.context.ConversationScoped;
import java.io.Serializable;
import java.util.HashMap;
import javax.enterprise.context.Conversation;
import javax.inject.Inject;

/**
 *This class is used to interact with the file basket.xhtml
 * It provides methods to render the web page
 * @author Guillaume
 */
@Named(value = "basketManager")
@ConversationScoped
public class BasketManager implements Serializable {

    @Inject
    private Basket basket;
    private HashMap<String, Integer> listOfGnomeTypeAndNumber = new HashMap<String, Integer>();
    @Inject
    private Conversation conversation;

    private void startConversation() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

    /** 
     * Creates a new instance of BasketManager 
     */
    public BasketManager() {
    }

    /**
     * To obtain a HashMap where Key is a gnome type and Value is the number of this type of gnomes in the client basket
     * @return a HashMap
     */
    public HashMap<String, Integer> getListOfGnomeTypeAndNumber() {
        startConversation();
   
        listOfGnomeTypeAndNumber = basket.getListOfGnomeType();
        return listOfGnomeTypeAndNumber;
    }
    /**
     * To add to the basket a certain quantity of type of gnome 
     * @param type
     * @param amount 
     */
    public void addGnomeTypeX(String type, int amount) {
        Double price = basket.getPriceForGnomeTypeX(type);
        basket.addItemsToTheBasket(type, amount, price);
    }
    /**
     * To delete from the basket a quantity of gnomes with a given type
     * @param type
     * @param amount 
     */
    public void deleteGnomeTypeX(String type, int amount) {
        basket.deleteItemsToTheBasket(type, amount);
    }
    /**
     * To obtain the total price for all gnomes of a certain type in the basket
     * @param type
     * @return 
     */
    public Double valueForAllGnomeTypeX(String type) {
        return basket.valueForAllGnomeTypeX(type);
    }
    /**
     * To obtain the total price for all gnomes in the basket
     * @return 
     */
    public Double priceForAllGnome() {
        return basket.priceForAllGnome();
    }
    /**
     * Call this function when a client wants to buy what is in his basket
     */
    public void buy() {
        basket.buy();
    }
}
